<template>
  <div class="login_container">
    <div class="login_box">
        <!-- 头像区域 -->
        <div class="avatar_box" ref="avt">
            <img src="../assets/img/ca86e.jpg" alt="">
        </div>
        <!-- 登录表单区域 -->
        <!-- ref是引用,值是实例对象 操作dom获取这个元素 -->
        <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" class="login_form"> 
            <!-- 用户名 -->                       
            <el-form-item prop="username">
                <el-input v-model="loginForm.username" prefix-icon="iconfont icon-user"></el-input>
            </el-form-item>
            <!-- 密码 -->
            <el-form-item prop="password">
                <el-input type="password" v-model="loginForm.password" prefix-icon="iconfont icon-3702mima"></el-input>
            </el-form-item>
            <el-form-item style="float:right">
                <el-button type="primary" @click="login">登录</el-button>
                <el-button type="info" @click="resetForm">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
  </div>
</template>

<script>
export default {

  data () {
    return {
        // 表单用的数据
        loginForm:{
            username:'admin',
            password:'123456'
        },
        // 校验表单规则
        loginRules:{
            username:[
                { required: true, message: '请输入用户名', trigger: 'blur' },
                { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
            ],
            password:[
                { required: true, message: '请输入正确的密码', trigger: 'blur' },
                { min: 6, max: 8, message: '长度在 6 到 8 个字符', trigger: 'blur' }
            ]
        }
    }
  },

  methods: {
      resetForm(){
        //   只要调用resetFields这个方法就可以重置表单
        // 表单组件才能调用
          this.$refs.loginFormRef.resetFields();
      },
      login(){
          this.$refs.loginFormRef.validate(async (flag)=>{
            if(!flag) return;
            console.log('校验成功');
            const res=await this.axios.post('login',this.loginForm) 
            console.log(res);
            if(res.meta.status==200){
                this.$mg.success('登录成功')
                sessionStorage.setItem('token',res.data.token)
                this.$router.push('/home')
            
            }else{
                this.$mg.error(res.meta.msg);
            }
              
          })
      }
  },

  created () {
      console.log(this);
      
  }
}
</script>

// scoped 使样式只在本组件中起作用
<style lang='less' scoped>
.login_container{
    width: 100%;
    height: 100%;
    background-color: #2b4b6b;
    .login_box{
        position: absolute;
        left: 50%;
        top: 50%;
        width: 450px;
        height: 300px;
        background-color: #fff;
        transform: translate(-50%,-50%);
        border-radius: 5px;
        .avatar_box{
            height: 130px;
            width: 130px;
            background-color: #ddd;
            border: 1px solid #eee;
            border-radius: 50%;
            padding: 10px;
            box-shadow: 0 0 30px #ddd;
            position: absolute;
            left: 50%;
            top: 0;
            // transform: translate(-50%,-50%);
            animation: ami_img 2s linear infinite;
                img{
                    width: 100%;
                    height: 100%;
                    border-radius: 50%;
                    background-color: #ddd;

                }
        }
        .login_form{
            position: absolute;
            bottom: 0;
            width: 100%;
            box-sizing: border-box;
            padding: 0 20px;
                
        }
    }
}

@keyframes ami_img {
    0%{
        background-color: red;
        transform:translate(-50%,-50%) rotate3d(1,1,0,0deg);
    }
    25%{
        background-color: orangered;
        transform:translate(-50%,-50%) rotate3d(1,1,0,120deg);
    }
    50%{
        background-color: yellow;
        transform:translate(-50%,-50%) rotate3d(1,1,0,360deg);
    }
    75%{
        background-color: lightgreen;
        transform:translate(-50%,-50%) rotate3d(0,1,1,120deg);
    }
    100%{
        background-color: blue;
        transform:translate(-50%,-50%) rotate3d(0,1,1,360deg);
    }
}


</style>
